// pages/productxq/productxq.js
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    promodel:[],
    quantity: {
      count: 1,
      min: 1,
      max: 20,
      delStatus: 'disabled',
      addStatus: 'normal'
    },
    product_sku: "",
    hasSku: false,
    Sku: [],
    goods: [],
    circular: true,
    //是否显示画板指示点  
    indicatorDots: true,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    var that = this;
    wx.request({
      url: 'https://rjgs.sz81jk.com/api/VYwbangAPI.ashx',
      method: "post",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        "action": "getProductDetails",
        "productId": option.id,
        'userid': 0
      },
      dataType: "json",
      success: function (res) {
        console.log(res.data);
        var product;
        if (res.data.length == 2) {
          product = res.data[0];
        } else {
          product = res.data;
        }
        product["count"] = 1;
        product["total"] = product.DefaultSku.SalePrice;
        var image = [];
        image.push(product.ImageUrl1);
        image.push(product.ImageUrl2);
        image.push(product.ImageUrl3);
        image.push(product.ImageUrl4);
        image.push(product.ImageUrl5);
        for (var i = 0; i < image.length; i++) {
          if (image[i] == "") {
            image.splice(i, 1);
            i--;
          }
        }
        that.setData({
          goods: product,
          product_sku: product.DefaultSku,
          imgUrls: image,
        })
        console.log(that.data.goods);
        if (product.HasSKU) {
          that.setData({
            hasSku: true,
            Sku: res.data[1]
          })
        }
        if (product.Description != null) {
          WxParse.wxParse('article', 'html', product.Description, that, 0);
        }
      }
    })
    wx.request({
      url: 'https://rjgs.sz81jk.com/api/VshopProcess.ashx',
      method: "post",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        "action": "CheckFavorite",
        "productId": option.id,
        'userid': 0 //wx.getStorageSync('userid')
      },
      dataType: "json",
      success: function (res) {
        if (res.data.success) {
          that.setData({
            isLike: true,
          });
        }
      }
    })
  },
  /* 减数 */
  delCount: function (e) {
    var count = this.data.goods.count;
    // 商品总数量-1
    if (count > 1) {
      this.data.goods.count--;
    }
    // 将数值与状态写回  
    this.setData({
      goods: this.data.goods
    });
    this.priceCount();
  },
  /* 加数 */
  addCount: function (e) {
    var count = this.data.goods.count
    // 商品总数量-1  
    if (count < this.data.product_sku.Stock) {
      this.data.goods.count++;
    }
    // 将数值与状态写回  
    this.setData({
      goods: this.data.goods
    });
    this.priceCount();
  },
  //价格计算
  priceCount: function (e) {
    this.data.goods.total = this.data.product_sku.SalePrice * this.data.goods.count;
    this.setData({
      goods: this.data.goods
    })
  },
  changeSku: function (e) {
    var skuId = e.currentTarget.dataset.skuid;
    var sku = this.data.goods.Skus[skuId];
    this.data.goods.total = sku.SalePrice;
    this.setData({
      product_sku: sku,
      goods: this.data.goods
    })
    this.priceCount();
  },
  // 收藏
  addLike() {
    var isLike = this.data.isLike;
    console.log(isLike);
    var action = 'AddFavorite';
    var msg = "收藏成功"
    if (isLike) {
      action = "DelFavorite";
      msg = "取消收藏";
    }
    this.setData({
      isLike: !this.data.isLike
    })

    console.log(action);
    var that = this;
    wx.request({
      url: 'https://rjgs.sz81jk.com/api/VshopProcess.ashx',
      method: "post",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        'action': action,
        'ProductId': that.data.goods.ProductId,
        'Userid': wx.getStorageSync('userid')
      },
      dataType: 'json',
      success: function (res) {
        if (res.data.success) {
          wx.showToast({
            title: msg,
            icon: 'success',
            duration: 2000
          })
        }
      }
    })
  },
  // 跳到首页
  toIndex() {
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  // 立即购买
  immeBuy() {
    this.setData({
      showDialog: !this.data.showDialog
    });
  },
  toggleDialog: function () {
    this.setData({
      showDialog: !this.data.showDialog
    });
  },
  closeDialog: function () {
    this.setData({
      showDialog: false
    });
  },
  submitorder: function () {
    wx.navigateTo({
      url: '/pages/order/order?buyAmount=' + this.data.goods.count + '&product_sku=' + this.data.product_sku.SkuId + '&productId=' + this.data.goods.ProductId,
    })
  },
  addCar: function () {
    var that = this;
    wx.request({
      url: 'https://rjgs.sz81jk.com/api/VshopProcess.ashx',
      method: "post",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        "action": "AddToCartBySkus",
        'userid': wx.getStorageSync('userid'),
        'limitedTimeDiscountId': '0',
        'quantity': that.data.goods.count,
        'productSkuId': that.data.product_sku.SkuId,
        'Templateid': '0',
        'categoryid': that.data.goods.CategoryId
      },
      dataType: JSON,
      success: function (res) {
        if (JSON.parse(res.data).Status == "OK") {
          wx.showModal({
            title: '提示',
            content: '加入购物车成功',
            showCancel: false
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },  
  Addgwc:function(){
    console.log();
    var productSkuId = this.data.promodel.ProductId;
    var categoryid = this.data.promodel.CategoryId;
    wx.request({
      url: 'https://rjgs.sz81jk.com/api/VshopProcess.ashx',
      data:{
        action: "AddToCartBySkus", 
        limitedTimeDiscountId: 0, 
        quantity: 1,
        productSkuId: productSkuId , 
        Templateid: 0,
        categoryid: categoryid
      },
      success(res){
        if (res.data.Status =="OK"){
          wx.showToast({
            title:"已加入购物车",
            mask:true
          })
        }
        else if (res.data.Status == "0"){//商品已下架
         var tck1= wx.showToast({
            title:"商品已下架",
            mask:true
          })
        }
        else if (res.data.Status == "1") {//商品库存不足
          wx.showToast({
            title: "商品库存不足",
            mask:true
          })
        }
        else {//其它错误信息
          if (res.data.Status == "2"){
            wx.showToast({
              title: "用户未登录。",
              mask:true
            })
          }else{
            wx.showToast({
              title: res.data.Status,
              mask: true
            })
          }
        }      
      }
    })
  }
})